package com.sanjiahantang.youjiaclient.module.me.account.presenters;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.city.models.ICityModel;
import com.sanjiahantang.youjiaclient.module.city.models.impl.CityModel;
import com.sanjiahantang.youjiaclient.module.common.beans.ConditionBean;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.beans.UserBean;
import com.sanjiahantang.youjiaclient.module.common.models.IUserModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiaclient.module.common.views.impl.ShareSqcodeActivity;
import com.sanjiahantang.youjiaclient.module.me.account.views.AccountView;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;
import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;

/**
 * Created by zy on 2017/9/4.
 */

public class AccountPresenter extends BaseMvpPresenter<AccountView> {
    private ArrayList<String> listGender;
    private IUserModel userModel;
    private ICityModel cityModel;
    private  AccountView.OnLoadListener onLoadListener;
    private  String QrCode;
    public AccountPresenter() {
        userModel = new UserModel();
        cityModel = new CityModel();
    }
    public void init(Context context) {
        account(context);
    }
    //个人资料
    public void account(Context context) {
        Map<String, String> params = new HashMap<>();
        params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        params.put("phone", SPUtils.get(context, SPConfig.PHONE, "").toString());
        mView.showProgress();
        userModel.dopersonData(context, params, new AccountCallBack());
    }

    private class AccountCallBack implements CallBack<UserBean> {

        @Override
        public void onSuccess(UserBean response, Context context, Object tag) {
            if (mView == null) return;
            hideProgress();
            if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                mView.setPhone(response.getData().getUserDto().getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                if (response.getData().getUserDto().getUserName()==null){
                    mView.setName("");
                }else {
                    mView.setName(response.getData().getUserDto().getUserName());
                }
                if (response.getData().getUserDto().getJobName()==null){
                    mView.setJob(null);
                }else {
                    ConditionBean.SelectorData data = new ConditionBean.SelectorData();
                    data.setDicName(response.getData().getUserDto().getJobName());
                    data.setDicCode(response.getData().getUserDto().getJobCode());
                    data.setDicValue(response.getData().getUserDto().getJobCode());
                    mView.setJob(data);
                }
                if (response.getData().getUserDto().getAddress()==null){
                    mView.setLocate("未设置");
                }else {
                    mView.setLocate(response.getData().getUserDto().getAddress());
                }

                if (response.getData().getUserDto().getLikeName()==null){
                    mView.setHobby(null,null);
                }else {
                    mView.setHobby(response.getData().getUserDto().getLikeCode(),response.getData().getUserDto().getLikeName());
                }

                String ghtUserId = response.getData().getUserDto().getGHTUserId();
                if (TextUtils.isEmpty(ghtUserId)){
                    mView.setGHTUserId("无");
                }else{
                    mView.setGHTUserId(ghtUserId);
                }
                QrCode =response.getData().getUserDto().getQrCode();
                ConfigValue.dutylist= (ArrayList<ConditionBean.SelectorData>) response.getData().getDutyList();
                ConfigValue.favoritelist= (ArrayList<ConditionBean.SelectorData>) response.getData().getLikeList();
                if (response.getData().getUserDto().getSex() != null) {
                    String gender;
                    switch (Integer.parseInt(response.getData().getUserDto().getSex())) {
                        case 0:
                            gender = "男";
                            break;
                        case 1:
                            gender = "女";
                            break;
                        default:
                            gender = "保密";
                            break;
                    }
                    mView.setGender(gender);
                } else {
                    mView.setGender("未设置");
                }
            }
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }

    private void hideProgress() {
        if (mView != null) mView.hideProgress();
    }

    public ArrayList<String> getGenderList() {
        if (listGender == null) {
            listGender = new ArrayList<>();
            listGender.add("男");
            listGender.add("女");
            listGender.add("保密");
        }
        return listGender;
    }

    public void setOnLoadListener(AccountView.OnLoadListener onLoadListener) {
        this.onLoadListener = onLoadListener;
    }
    public void loadPersonCityList(Context context, BaseView.OnLoadListener listener) {
        cityModel.getAllArea(context, listener);
    }

    public void goPageQR(Context context) {
        Intent intent=new Intent(context, ShareSqcodeActivity.class);
        intent.putExtra("QRCODE",QrCode);
        context.startActivity(intent);

    }


    //更新资料
    public void setData(final Context context,
                        String username, String txtGender, String txtJobcode, String txtJob, String txtLocation, String likecode, String txtInterest, String key,
                        final BaseView.OnLoadListener listener
    ) {

        Map<String, Object> map = new HashMap<>();
        map.put("stateId", key);
        map.put("token", ConfigValue.DATA_KEY);
        map.put("userName", username);
        map.put("phone", SPUtils.get(context, SPConfig.PHONE, "").toString());
        map.put("sex", txtGender);
        map.put("jobCode", txtJobcode);
        map.put("jobName", txtJob);
        map.put("address", txtLocation);
        map.put("likeCode",likecode);
        map.put("likeName", txtInterest);

        userModel.updatePerson(context, map, new CallBack<StringBean>(){

            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    Utils.showToast(context, response.getMessage());
                    if (listener != null){
                        listener.onLoad(null);
                    }
                }
            }
            @Override
            public void onFailure(String message) {

                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context,"修改失败");
                System.out.println(message);


            }
        });
    }
    public void setData(Context context,
                        String username, String txtGender, String txtJobcode, String txtJob, String txtLocation, String likecode, String txtInterest, String key){
        setData(context,username,txtGender,txtJobcode,txtJob,txtLocation,likecode,txtInterest,key,null);
    }
}
